<?php
/**
 * Created by PhpStorm.
 * User: hebidu
 * Date: 15/11/19
 * Time: 15:04
 */

namespace Api\Service;


class AliBaichuanService {


    public function sendMessage($mobile,$context=array()){

        vendor('TopSdk',APP_PATH.'Api/Vendor/Alibaichuan/','.php');
        $domain = $context['domain'];
        $signature_id = $context['signature_id'];
        $template_id = $context['template_id'];

        if(!empty($domain)){
            $domain = "unknown";
        }

        $c = new \TopClient();
        $c->appkey = $context['appkey'];
        $c->secretKey = $context['secretKey'];

        $c->format = "json";
        $req = new \OPENS();
        $send_message_request = new \SendMessageRequest();
        $send_message_request->mobile_limit="5";  //5次 mobile_limit_in_time内5次
        $send_message_request->external_id="12345";
        $send_message_request->mobile_limit_in_time="3600";//1小时
        $send_message_request->template_id= $template_id;
        $send_message_request->signature_id= $signature_id;
        $send_message_request->domain = $domain;
        $send_message_request->mobile=$mobile;
        $send_message_request->context =$context;
        $req->setSendMessageRequest(json_encode($send_message_request));
        $resp = $c->execute($req);
        if(isset($resp['result'])){
            //返回信息
            $result = $resp['result'];
            if(isset($result['code']) && $result['code'] == 1 && $result['successful']){
                return array('status'=>true,'info'=>"短信已发送,请注意查收!");
            }else{
                return array('status'=>false,'info'=>"短信发送失败,请重试!");
            }

        }else{
            //发生异常
            if(isset($resp['sub_msg'])){
                return array('status'=>false,'info'=>$resp['sub_msg']);
            }else{
                return array('status'=>false,'info'=>$resp['msg']);
            }
        }



    }

}